/**
 * 交易币别维护
 * @author yuan_rz
 * @date 2019/01/11 11:15
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        element,
        vueData = {

        },
        table_where = {

        },
        cols = [[
              {type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'ROW_ID', width: 60, title: '序号', fixed: 'left',hide : true}
            , {field: 'GWAC001', width: 80, title: '币别编号', fixed: 'left', sort: true, search: 'default'}
            , {field: 'GWACL003', width: 80, title: '币别名称',search: 'default'}
            , {field: 'GWACL004', width: 80, title: '助记码'}
            , {field: 'GWAC002', width: 80, title: '最小面额',filterDatas: app.datas_('base_gwai210_gwac002')}
            , {field: 'GWAC003', width: 80, title: '货币符号',filterDatas: app.datas_('base_gwai210_gwac003')}
            , {field: 'GWACACTI', width: 80, title: '有效否',filterDatas: app.datas_('sys_y_n')}
            , {
                width: 60,
                title: '',
                toolbar: '#script-gwai210setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwai210SettingAction,
        moduleName = '交易币别',
        editArea = ['360px', '430px']
    ;

    var gwai210 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai210',
                _searchs: [{
                    id: 'gwai210', tableId: 'gwai210_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwai210_edit', tpl: 'base/gwai210_edit'
                }]
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai210_table'),
                    url: g_ctxPath + 'base/gwai210/getListData',
                    exportsUrl: g_ctxPath + 'base/gwai210/exportListData',
                    exportsName: moduleName + '数据',
                    title: moduleName + '表',
                    method: 'post',
                    id: 'gwai210_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: false,
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwai210setting-action').innerHTML).render({}, function (string) {
                    gwai210SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwai210_table)', function (obj) {

                    layer.tips(gwai210SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            tipsIndex = index;
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '1':
                                        self._openTplFull({
                                            tag: 'gwai210_edit',
                                            tagData: {
                                                obj : obj,
                                                moduleName : moduleName
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: moduleName + '编辑',
                                                area: editArea
                                            }
                                        });
                                        break;
                                    case '2':
                                        app.confirm({
                                            msg: '是否删除选择的数据？',
                                            ok: function () {
                                                self.deleteData(obj);
                                            }
                                        });
                                        break;
                                }
                                layer.close(index);
                                tipsIndex = null;
                            });
                        }
                    });
                    return false;
                });

                //监听行双击事件
                table.on('rowDouble(gwai210_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwai210_edit',
                        tagData: {
                            obj : obj,
                            moduleName : moduleName,
                            is_detail:true
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '编辑',
                            area: editArea
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwai210_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai210_edit',
                                tagData: {
                                    is_add : true,
                                    moduleName : moduleName
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '编辑',
                                    area: editArea
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.deleteDataBatch();
                                }
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai210_table', where: table_where});
        },
        deleteData: function (obj) {
            app.ajax({
                xurl: 'base/gwai210/deleteData',
                xdata: obj.data,
                loading: true,
                okmsg: moduleName +'信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },
        deleteDataBatch: function(){
            var checkStatus = table.checkStatus('gwai210_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('base_gwai210');
                    _temp.GWACENT = v.GWACENT;
                    _temp.GWAC001 = v.GWAC001;
                    _temp.GWACLENT = v.GWACLENT;
                    _temp.GWACL001 = v.GWACL001;
                    _temp.GWACL002 = v.GWACL002;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'base/gwai210/deleteDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: moduleName +'信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    searchId: 'gwai210',
                    methods: {

                    }
                }]
            });

            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new gwai210();

});
